﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XRPGLibrary;
using XRPGLibrary.SpriteClasses;
using XRPGLibrary.TileEngine;

namespace myRPG.GameScreens.Combat
{
    public class Combat:BaseGameState
    {
        FightingPlayer player;
        List<Monster> monsters;
        Camera camera;
        SpriteBatch spriteBatch;

        public Combat(Game game,GameStateManager manager)
            : base(game, manager)
        { }

        protected override void LoadContent()
        {
            camera = new Camera(GameRef.ScreenRectangle);
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            Texture2D img_player = Game.Content.Load<Texture2D>(@"img\Combat\princessFighting");
            Dictionary<AnimationKey, Animation> animations = new Dictionary<AnimationKey, Animation>();
            Animation right = new Animation(8, 120, 100, 0, 100);
            animations.Add(AnimationKey.Right, right);
            Animation atkRight = new Animation(8, 120, 100, 0, 300);
            animations.Add(AnimationKey.AttackRight, atkRight);
            player = new FightingPlayer(img_player, animations);

            monsters = new List<Monster>();
            Monster monster;
            Texture2D img_monster = Game.Content.Load<Texture2D>(@"img\Combat\knight");
            animations = new Dictionary<AnimationKey, Animation>();
            Animation left = new Animation(4, 90, 90, 0, 90);
            animations.Add(AnimationKey.Left, left);
            monster = new Monster(img_monster, animations);
            monster.Position = new Vector2(800, 500);
            monsters.Add(monster);
            monster = new Monster(img_monster, animations);
            monster.Position = new Vector2(700, 400);
            monsters.Add(monster);
            monster = new Monster(img_monster, animations);
            monster.Position = new Vector2(400, 400);
            monsters.Add(monster);

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            Vector2 motion = new Vector2();
            if (InputHandler.KeyDown(Keys.W))
            {
                //player.State = FightState.Moving;
                motion.Y = -1;
            }
            //按S键，精灵向下移动
            else if (InputHandler.KeyDown(Keys.S))
            {
                //player.State = FightState.Moving;
                motion.Y = 1;
            }
            if (InputHandler.KeyDown(Keys.A))
            {
                //player.State = FightState.Moving;
                motion.X = -1;
            }
            else if (InputHandler.KeyDown(Keys.D))
            {
                //player.State = FightState.Moving;
                motion.X = 1;
            }
            if (InputHandler.KeyDown(Keys.Space))
            {
                player.Attack();
            }
            if (motion != Vector2.Zero)
            {
                //player.IsAnimating = true;
                motion.Normalize();
                player.Position += motion * player.Speed;//单位化方向向量，并乘以速度，重置精灵位置
                //player.LockToMap();
            }
            player.Update(gameTime);
            foreach (Monster m in monsters)
            {
                m.Update(gameTime);
                m.CheckCollision(player.Weapon);
            }

            for (int i = 0; i<monsters.Count; i++)
            {
                if (monsters[i].IsDead)
                    monsters.RemoveAt(i);
            }

            if (monsters.Count == 0)
                StateManager.PopState();
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            foreach (Monster m in monsters)
                m.Draw(gameTime, spriteBatch, camera);
            player.Draw(gameTime, spriteBatch, camera);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
